import torch
Tensor = torch.Tensor

def l2_loss(
        n: int
):
    """L2 Loss function with one order scheme to calculate the integration.
    sqrt(intgrate(|input - target|^2))
    return the mean value on the batched targets and inputs
    """
    h = 2 / (n - 1)
    h2 = h**2

    def func(input, target):
        batched_vals = torch.sqrt(torch.sum((input - target) ** 2 * h2, dim=(1, 2, 3)))
        return batched_vals.mean()
    return func

if __name__ == '__main__':
    x = torch.rand(5, 1, 32, 32)
    y = torch.rand(5, 1, 32, 32)
    loss_fn = l2_loss(32)
    print(loss_fn(x, y))